package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.peakstronghold.CrossPeakStrongholdDefendUnionData;
import com.yanqu.road.entity.activity.peakstronghold.CrossPeakStrongholdUnionData;
import com.yanqu.road.entity.activity.peakstronghold.PeakStrongholdDefendPatrons;
import com.yanqu.road.entity.activity.peakstronghold.config.PeakStrongHoldConfig;
import com.yanqu.road.entity.activity.peakstronghold.config.PeakStrongholdBaseConfig;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.activity.PeakStrongholdWarProto;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdActivityMgr;

import java.util.List;

public class CrossPeakStrongholdPb {
    public static PeakStrongholdWarProto.PeakEliminateUnionBaseDataMsg.Builder buildUnionBaseDataMsg(CrossPeakStrongholdUnionData unionData, CrossPeakStrongholdDefendUnionData defendUnionData, PeakStrongHoldConfig config, int rank) {
        PeakStrongholdWarProto.PeakEliminateUnionBaseDataMsg.Builder msg = PeakStrongholdWarProto.PeakEliminateUnionBaseDataMsg.newBuilder();
        msg.setUnionId(unionData.getUnionId());

        PeakStrongholdBaseConfig baseConfig = config.getBaseConfigByExp(unionData.getTotalDonateExp());
        msg.setStar(baseConfig.getLevel());

        if (rank == 0) {
            msg.setRank(defendUnionData.getGroupRank());
        } else {
            msg.setRank(rank);
        }

        msg.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(unionData.getUnionBaseInfo()));
        msg.setUnionId(unionData.getUnionId());
        msg.setStatus(unionData.getStatus());
        msg.setBlood(CrossStrongholdActivityMgr.calcEliminateCurBlood(defendUnionData.getCurBlood(), defendUnionData.getTotalBlood()));
        msg.setGroup(defendUnionData.getGroup());
        msg.setRoundDamage(defendUnionData.getTotalDamage().toString());
        msg.setRoundScore(defendUnionData.getGroupScore().toString());
        return msg;
    }

    public static PeakStrongholdWarProto.PeakEliminateChallengePatronsDataMsg.Builder buildPatronsDataMsg(UserBaseInfo userBaseInfo, PeakStrongholdDefendPatrons defendPatrons) {
        PeakStrongholdWarProto.PeakEliminateChallengePatronsDataMsg.Builder defendPatronMsg = PeakStrongholdWarProto.PeakEliminateChallengePatronsDataMsg.newBuilder();
        defendPatronMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        defendPatronMsg.setPatronsId(defendPatrons.getPatronsId());
        defendPatronMsg.setSkinId(defendPatrons.getSkinId());
        defendPatronMsg.setLevel(defendPatrons.getLevel());
        defendPatronMsg.setStageLevel(defendPatrons.getStageLv());
        defendPatronMsg.setPatronsBlood(CrossStrongholdActivityMgr.calcEliminateCurBlood(defendPatrons.getCurBlood(), defendPatrons.getTotalBlood()));
        defendPatronMsg.setUserId(defendPatrons.getUserId());

        return defendPatronMsg;
    }

    public static PeakStrongholdWarProto.CrossPeakHoldEliminateUnionListMsg.Builder buildEliminateUnionListMsg(List<CrossPeakStrongholdUnionData> unionDataList, int round, int unionGroup) {
        PeakStrongholdWarProto.CrossPeakHoldEliminateUnionListMsg.Builder msg = PeakStrongholdWarProto.CrossPeakHoldEliminateUnionListMsg.newBuilder();
        msg.setRound(round);
        for (CrossPeakStrongholdUnionData unionData : unionDataList) {
            if (unionData.getLastRound() != round) {
                msg.addOutUnionIds(unionData.getUnionId());
                continue;
            }
            PeakStrongholdWarProto.PeakHoldUnionRoundEndMsg.Builder unionMsg = PeakStrongholdWarProto.PeakHoldUnionRoundEndMsg.newBuilder();
            unionMsg.setUnionId(unionData.getUnionId());
            msg.addRiseUpUnions(unionMsg);
        }
        msg.setUnionGroup(unionGroup);

        return msg;
    }
}
